
#if defined(APP_CAR_PETROL_BLOCKING)

#include "main.h"

#define   TIMEOUT_TO_BLOCK		600 // x100ms = 1 min

#define   IGN_INPUT			InputsState.INP_6	/* RF1 */

unsigned long TimeCnt;

/* const unsigned char Car_UnblockPetrol[] = "MSblock OS 1"; */
/* const unsigned char Car_BlockPetrol[]   = "MSunblock OS 0"; */

const unsigned char Car_UnblockPetrol[] = "MSb1975 OS 0";
const unsigned char Car_BlockPetrol[]   = "MSu1975 OS 1"; 
const unsigned char Car_UnblockDoor[]   = "MSdoor1975 OS -2 20"; /* (-2) don't change state of REL_1 (OUT_1) */


enum
{
	SET_COMMAND_MACROS_1 = 20,
	SET_COMMAND_MACROS_2,
	SET_COMMAND_MACROS_3,
	SET_EXCEPTION_1,
	SET_EXCEPTION_2,
	SET_EXCEPTION_3
};

/* #define   CAR_PETROL_STATUS_LED		RF2 /\* output *\/ */
/* #define   CAR_PETROL_STATUS_LED_DIR	TRISF2 /\* output *\/ */

#define   CAR_PETROL_STATUS_LED		OUT_5 /* output */

#define   CAR_PETROL_CONTROL			OUT_1

#define   CAR_PETROL_CONTROL__BLOCK			0
#define   CAR_PETROL_CONTROL__UNBLOCK		HIGH

#define   STATUS_BLOCKED			1
#define   STATUS_UNBLOCKED			0
#define   STATUS_ERROR				OUT_BLINK

#define   ALARM_STATUS__ARM		1
#define   ALARM_STATUS__DISARM	0


#define   EVENT__INP_DOOR				INPUT_1_DOWN						
#define   EVENT__INP_IGN				INPUT_6_UP						
#define   EVENT__INP_ENGINE			INPUT_2_DOWN						

unsigned char AlarmState, AlarmStatus, CarAlarm_Timeout;

const char* AlarmMessage[] = 
{
	"SML1 Alarm: IGNITION",
	"SML1 Alarm: ENGINE",
	"SML1 Alarm: DOOR"
};

enum
{
	ALARM_MESSAGE__IGNITION = 0,
	ALARM_MESSAGE__ENGINE,
	ALARM_MESSAGE__DOOR
};

signed char AlarmMessage_Number;


void Car_RF ( void )
{
	
}

void CarAlarm ( void )
{
	switch( AlarmState )
	{
		case ST_RESET:
			Debug("\nCarAlarm: STARTED ");
			AlarmStatus = ALARM_STATUS__ARM;
			AlarmState = IDLE;
			AlarmMessage_Number = -1;
			break;

		case IDLE:
			if ( AlarmStatus == ALARM_STATUS__ARM )
			{
				if ( InputsEvent == EVENT__INP_IGN )
				{
					AlarmMessage_Number = ALARM_MESSAGE__IGNITION;
				}
				else
					if ( InputsEvent == EVENT__INP_ENGINE )
					{
						AlarmMessage_Number = ALARM_MESSAGE__ENGINE;
					}
					else
						if ( InputsEvent == EVENT__INP_DOOR )
						{
							AlarmMessage_Number = ALARM_MESSAGE__DOOR;
						}

				if ( AlarmMessage_Number != -1 )
				{
					AlarmState = SEND_ALARM_MESSAGE;
					CarAlarm_Timeout = 5;
				}
			}
			break;

		case SEND_ALARM_MESSAGE:
			if ( ExeSysCommand(AlarmMessage[AlarmMessage_Number]) == TRUE )
			{
				AlarmMessage_Number = -1;
				AlarmState = IDLE;
			}
			else
				if ( Time_1000ms && --CarAlarm_Timeout == 0 )
				{
					Debug("\nCarAlarm. Send message: TIMEOUT");
					AlarmState = IDLE;
				}
			break;

		default:
			AlarmState = ST_RESET;
	}
}

void CarPetrolBlocking ( void )
{
	/* Inputs Handler. Configurate Inputs of Ignition: if 1 min is ACTIVE - block petrol (set RELAY_1 in ON ) */
	
	/* SMS Handler. If receive SMS whith command UNBLOCK PETROL - - unblock petrol (set RELAY_1 in OFF ) */
	
	static unsigned char State = ST_START, Delay;
	

	if ( Delay )
	{
		if ( Time_100ms )
		{
			Delay--;
		}

		return ;
	}


	switch( State )
	{
		case ST_RESET:
		case ST_START:
			CAR_PETROL_CONTROL = CAR_PETROL_CONTROL__BLOCK;
			TimeCnt = 0;
			Debug("CarPetrolBlocking: STARTED ");
			State = SET_COMMAND_MACROS_1;
			/* 			Format: MSMacroName Command */

/* Example: */
/* MSDoor1 OS 0 30 0 0	 */

/* After this command you can send Door1 instead of OS 0 30 0 0 */

/* ! Note: Into MacroName first two characters can't be high register;  */
/* 	OPenDoor - not correct;                                             */
/* 	opendoor - correct; */
/* 	oPEndOOr - correct; */

			break;

		case SET_COMMAND_MACROS_1:
			if ( ExeSysCommand( Car_BlockPetrol ) == TRUE )
			{
				State = SET_COMMAND_MACROS_2;
			}
			Delay = 1;
			break;

		case SET_COMMAND_MACROS_2:
			if ( ExeSysCommand( Car_UnblockPetrol ) == TRUE )
			{
				State = SET_COMMAND_MACROS_3;
			}
			Delay = 1;
			break;

		case SET_COMMAND_MACROS_3:
			if ( ExeSysCommand( Car_UnblockDoor ) == TRUE )
			{
				State = SET_EXCEPTION_1;
			}
			Delay = 1;
			break;

		case SET_EXCEPTION_1:
			if ( ExeSysCommand( "AEb1975" ) == TRUE )
			{
				State = SET_EXCEPTION_2;
			}
			Delay = 1;
			break;

		case SET_EXCEPTION_2:
			if ( ExeSysCommand( "AEu1975" ) == TRUE ) 
			{
				State = SET_EXCEPTION_3;
			}
			Delay = 1;
			break;

		case SET_EXCEPTION_3:
			if ( ExeSysCommand( "AEdoor1975" ) == TRUE )
			{
				State = IDLE;
			}
			Delay = 1;
			break;


		case IDLE:
			/*  Wait for Incoming Call for UNBLOCK petrol*/
			if ( IncomingCall_Event == INCOMING_CALL_EVENT__PHONE_FOUND )
			{
				CAR_PETROL_CONTROL = CAR_PETROL_CONTROL__UNBLOCK;
				AlarmStatus = ALARM_STATUS__DISARM;
				OUT_2 = 20;
				Debug("\nCarPetrolBlocking: UNBLOCK");
			}

			/* show status by LED CAR_PETROL_STATUS_LED */
			if ( CAR_PETROL_STATUS_LED == STATUS_UNBLOCKED && CAR_PETROL_CONTROL == CAR_PETROL_CONTROL__BLOCK  )
			{
				CAR_PETROL_STATUS_LED = STATUS_BLOCKED;
				AlarmStatus = ALARM_STATUS__ARM;
				Debug("\nCarPetrolBlocking status: BLOCKED");
			} 
			else
				if ( CAR_PETROL_STATUS_LED == STATUS_BLOCKED && CAR_PETROL_CONTROL == CAR_PETROL_CONTROL__UNBLOCK )
				{
					TimeCnt = 0;
					CAR_PETROL_STATUS_LED = STATUS_UNBLOCKED;
					Debug("\nCarPetrolBlocking status: UNBLOCKED");
				}

			/* Wait for Input to blok petrol */
			if ( Time_100ms )
			{
				if ( IGN_INPUT == LOW )
				{
					if ( ++TimeCnt == TIMEOUT_TO_BLOCK )
					{
						Debug("\nCarPetrolBlocking: BLOCKED");
						AlarmStatus = ALARM_STATUS__ARM;
						CAR_PETROL_CONTROL = CAR_PETROL_CONTROL__BLOCK;  /* block petrol */
					}
				}
				else
				{
					TimeCnt = 0;
				}
			}

			if ( CellNetwork_Status != CELL_NETWORK__REGISTRATION_OK )
			{
				CAR_PETROL_STATUS_LED = STATUS_ERROR; /* blink with external Status LED */
			}
			else
				if ( CAR_PETROL_STATUS_LED == STATUS_ERROR )
				{
					CAR_PETROL_STATUS_LED = STATUS_BLOCKED;
				}
			
			if ( Time_1Min )		  /* Monitor */
			{
				if ( CAR_PETROL_STATUS_LED == STATUS_BLOCKED )
				{
					Debug("\nCarPetrolCtrl status: BLOCKED; during %d sec", (TimeCnt/10));
				}
				else
				{
					Debug("\nCarPetrolCtrl status: UNBLOCKED");
				}
			}
			break;

		default:
			State = ST_RESET;
	}

}

#endif

